package k3;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;

public class Utils{

	public static ArrayList<String> readFile(String fileName) throws IOException{
		FileInputStream fstream = null;
		BufferedReader br = null;
		ArrayList<String> out = new ArrayList<String>();
		try {
			fstream = new FileInputStream("src/sample/"+fileName+".in");
			DataInputStream in = new DataInputStream(fstream);
			br = new BufferedReader(new InputStreamReader(in));
		} catch (FileNotFoundException e) {
			System.err.println("File does not exist: " + e.getMessage());
			e.printStackTrace();
		}
		String line = null;
		try {
			while((line = br.readLine()) != null) {
				out.add(line);
			} 
		}catch (IOException e) {
				System.err.println("IOException: " + e.getMessage());
			}
			// Closing stream
			br.close();
		return out;
	}
	
	public static void writeFile(ArrayList<String> lines, String fileName){
		FileWriter fstream = null;
		BufferedWriter bw = null;
		try {
			fstream = new FileWriter("src/sample/"+fileName+".out");
			// Save numbers to file.
			bw = new BufferedWriter(fstream);
			for (String line : lines) {
				bw.write(line);
				bw.newLine();
			}
			bw.close();
			System.out.println("Outcome saved: " + fileName);
		} catch (IOException e) {
			System.err.println("Failed to write file: " + e.getMessage());
			e.printStackTrace();
		}
	}
}
